package cn.iocoder.yudao.module.warehouseoperationlog.controller.admin.warehouseoperationlog;

import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.warehouseoperationlog.controller.admin.warehouseoperationlog.vo.*;
import cn.iocoder.yudao.module.warehouseoperationlog.dal.dataobject.warehouseoperationlog.WarehouseOperationLogDO;
import cn.iocoder.yudao.module.warehouseoperationlog.service.warehouseoperationlog.WarehouseOperationLogService;

@Tag(name = "管理后台 - 库房操作记录表（记录库房相关操作，支撑审计追溯）")
@RestController
@RequestMapping("/warehouseoperationlog/warehouse-operation-log")
@Validated
public class WarehouseOperationLogController {

    @Resource
    private WarehouseOperationLogService warehouseOperationLogService;

    @PostConstruct
    public void init() {
        System.out.println(1111);
    }

    @PostMapping("/create")
    @Operation(summary = "创建库房操作记录表（记录库房相关操作，支撑审计追溯）")
    @PreAuthorize("@ss.hasPermission('warehouseoperationlog:warehouse-operation-log:create')")
    public CommonResult<Long> createWarehouseOperationLog(@Valid @RequestBody WarehouseOperationLogSaveReqVO createReqVO) {
        return success(warehouseOperationLogService.createWarehouseOperationLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库房操作记录表（记录库房相关操作，支撑审计追溯）")
    @PreAuthorize("@ss.hasPermission('warehouseoperationlog:warehouse-operation-log:update')")
    public CommonResult<Boolean> updateWarehouseOperationLog(@Valid @RequestBody WarehouseOperationLogSaveReqVO updateReqVO) {
        warehouseOperationLogService.updateWarehouseOperationLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库房操作记录表（记录库房相关操作，支撑审计追溯）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('warehouseoperationlog:warehouse-operation-log:delete')")
    public CommonResult<Boolean> deleteWarehouseOperationLog(@RequestParam("id") Long id) {
        warehouseOperationLogService.deleteWarehouseOperationLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除库房操作记录表（记录库房相关操作，支撑审计追溯）")
                @PreAuthorize("@ss.hasPermission('warehouseoperationlog:warehouse-operation-log:delete')")
    public CommonResult<Boolean> deleteWarehouseOperationLogList(@RequestParam("ids") List<Long> ids) {
        warehouseOperationLogService.deleteWarehouseOperationLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库房操作记录表（记录库房相关操作，支撑审计追溯）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('warehouseoperationlog:warehouse-operation-log:query')")
    public CommonResult<WarehouseOperationLogRespVO> getWarehouseOperationLog(@RequestParam("id") Long id) {
        WarehouseOperationLogDO warehouseOperationLog = warehouseOperationLogService.getWarehouseOperationLog(id);
        return success(BeanUtils.toBean(warehouseOperationLog, WarehouseOperationLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库房操作记录表（记录库房相关操作，支撑审计追溯）分页")
    @PreAuthorize("@ss.hasPermission('warehouseoperationlog:warehouse-operation-log:query')")
    public CommonResult<PageResult<WarehouseOperationLogRespVO>> getWarehouseOperationLogPage(@Valid WarehouseOperationLogPageReqVO pageReqVO) {
        PageResult<WarehouseOperationLogDO> pageResult = warehouseOperationLogService.getWarehouseOperationLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarehouseOperationLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库房操作记录表（记录库房相关操作，支撑审计追溯） Excel")
    @PreAuthorize("@ss.hasPermission('warehouseoperationlog:warehouse-operation-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarehouseOperationLogExcel(@Valid WarehouseOperationLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarehouseOperationLogDO> list = warehouseOperationLogService.getWarehouseOperationLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库房操作记录表（记录库房相关操作，支撑审计追溯）.xls", "数据", WarehouseOperationLogRespVO.class,
                        BeanUtils.toBean(list, WarehouseOperationLogRespVO.class));
    }

    // WarehouseOperationLogController.java (添加新接口)
    @GetMapping("/page-with-warehouse")
    @Operation(summary = "获得库房操作记录与库房信息联查分页")
    @PreAuthorize("@ss.hasPermission('warehouseoperationlog:warehouse-operation-log:query')")
    public CommonResult<PageResult<WarehouseOperationLogWithWarehouseRespVO>> getWarehouseOperationLogWithWarehousePage(
            @Valid WarehouseOperationLogPageReqVO pageReqVO) {
        PageResult<WarehouseOperationLogWithWarehouseRespVO> pageResult = warehouseOperationLogService.getWarehouseOperationLogWithWarehousePage(pageReqVO);
        return success(pageResult);
    }
}